<?php
/**
 * LowCalVanilla Functions tests
 *
 * @package Extensions
 * @subpackage test
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

if (!defined('IN_VANILLA')) {
	require('./ini_test.php');

	define('STANDALONE_TEST', 1);
}

/**
 * LowCalVanilla File class tests
 * @package Extensions
 */
class TestFile extends UnitTestCase {

	var $Context;
	var $TestPath;
	var $Time;

	function setUp() {
		$this->Context = new TestContext();

		$this->TestPath = TEST_DIR . '/_files/extensions/Baz/baz.css';
		$this->Time = time() - 10000;
		touch($this->TestPath, $this->Time);
	}

	function testConstructor() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath,
			'rb');
		$this->assertEqual($File->Path, $this->TestPath);
		$this->assertTrue(is_resource($File->Fp));
		$File->Close();
	}

	function testWrite() {
		$file = new LowCalVanilla_File($this->Context, TEST_DIR . '/_files/cache/test.txt', 'wb');
		$file->Write('test.');
		$file->Write(' test again');
		$this->assertEqual('test. test again', file_get_contents($file->Path));
		$file->Unlink();
	}

	function testClose() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath,
			'rb');
		$this->assertTrue(is_resource($File->Fp));
		$File->Close();
		$this->assertFalse(is_resource($File->Fp));
		$this->assertFalse($File->_fp);
	}

	function testExist() {
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/extensions/Baz/baz');
		$this->assertFalse($File->Exist());

		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertTrue($File->Exist());
	}

	function testGetContents() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath,
			'rb');
		$this->assertEqual(
			'/*
test
*/',
			$File->GetContents());
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/test.txt', 'wb+');
		$File->Write('test. ');
		$this->assertEqual('test. ', $File->GetContents(True));
		$File->Write('write more.');
		$this->assertEqual('test. write more.', $File->GetContents());
	}

	function testGetLastModification() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertEqual($this->Time, $File->GetLastModification());
	}

	function testGetSize() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertEqual(10, $File->GetSize());
	}

	function testHasChanged() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertTrue($File->HasChanged($this->Time - 1));
		$this->assertFalse($File->HasChanged($this->Time));
	}

	function testOpen() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$File->Open('rb');
		$this->assertTrue(is_resource($File->Fp));
		$File->Close();
	}

		function testSetContent() {
		$this->assertFalse(file_exists(TEST_DIR . '/_files/cache/foo'));
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo', 'wb');
		$this->assertEqual(10, $File->Write('test write'));
		$File->Close();
		unset($File);
		$this->assertTrue(file_exists(TEST_DIR . '/_files/cache/foo'));
		$this->assertEqual('test write',
			file_get_contents(TEST_DIR . '/_files/cache/foo'));
		unlink(TEST_DIR . '/_files/cache/foo');

		$this->assertFalse(file_exists(TEST_DIR . '/_files/cache/foo'));
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo', 'wb');

		$File->Write('test');
		$File->Write(' write');
		$this->assertEqual(10, $File->GetSize());
		$File->Close();
		unset($File);
		$this->assertTrue(file_exists(TEST_DIR . '/_files/cache/foo'));
		$this->assertEqual('test write',
			file_get_contents(TEST_DIR . '/_files/cache/foo'));
		unlink(TEST_DIR . '/_files/cache/foo');
	}

	function testUnlink() {
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo', 'wb');
		$File->Write('test write');
		$this->assertTrue($File->Exist());
		$File->Unlink();
		$this->assertFalse($File->Exist());
	}

function testWritable() {
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo');
		$this->assertFalse($File->IsWritable());
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo/bar');
		$this->assertFalse($File->IsWritable());
	}

	function testLock() {
		$file = new LowCalVanilla_File($this->Context, TEST_DIR . '/_files/cache/test.txt', 'wb+');
		$sameFile = new LowCalVanilla_File($this->Context, TEST_DIR . '/_files/cache/test.txt');
		$file->Write('test. ');
		$this->assertFalse($sameFile->Open());
		$file->Close();
		$this->assertTrue($sameFile->Open());
		$this->assertEqual('test. ', $sameFile->GetContents(True));

		$this->assertFalse($file->Open('wb+'));
		$sameFile->Close();
		$file->Unlink();
	}
}

if (defined('STANDALONE_TEST')) {

	$test = new TestFile();
	$test->run(new HtmlReporter());
}
